#include<stdio.h>
#include<stdlib.h>
struct node
{ int value;
  struct node *next;
};
//FINDS THE MAXIMUM VALUE IN THE LIST
void max(struct node *list)
{int max_no;
 max_no=list->value;
 while((list->next)!=NULL)
 {if((list->value)>max_no)
   {max_no=list->value;}
  list=list->next;}
 printf("maximum number is:%d\n",max_no);
}
//FINDS THE MINIMUM VALUE IN THE LIST
void min(struct node *list)
{int min_no;
 min_no=list->value;
 while((list->next)!=NULL)
 {if((list->value)<min_no)
   {min_no=list->value;}
  list=list->next;}
 printf("minimum number is:%d\n",min_no);
}
//COUNTS THE NUMBER OF OCCURENCES OF AN ELEMENT
void count(struct node *list)
{int count=0,x;
 printf("enter no:\n");
 scanf("%d",&x);
 while(list!=NULL)
 {if(x==list->value)
  {count++;}
  list=list->next;
 }
 printf("total number of occurences of %d is %d \n",x,count);
}
//FINDS THE INDEX VALUE OF AN ELEMENT
void index1(struct node *list)
{int x,n=0,flag=0;
 printf("enter a number: \n");
 scanf("%d",&x);
 while((list->next)!=NULL)
 {if(x==(list->value))
   {printf("index value of first occurence of %d is %d \n",x,n);
    flag=1;
    break;}
  list=list->next;n++;
 }

 if(flag==0)
 printf("number doesn't exist \n");
}
//APPENDS AN ELEMENT IN THE LIST
struct node *append(struct node *list , int x)
{struct node *new_node,*p;
 new_node=malloc(sizeof(struct node));
 p=list;
 for(;;p=p->next)
 { if(p->next==NULL)
   { new_node->value=x;
     new_node->next=NULL;
     p->next=new_node;
     break;
    }
  }return list;
}
//REVERSES THE LIST
struct node *reverse(struct node *list)
{struct node *c,*p,*r;
 c=malloc(sizeof(struct node));
 c->value=list->value;
 c->next=NULL;
 p=c;
 list=list->next;
 while(list!=NULL)
{r=malloc(sizeof(struct node));
 r->value=list->value;
 r->next=p;
 p=r;
 list=list->next;}
 return r;}
//INSERTS AN ELEMENT AT THE "ith" POSITION
struct node* insert(struct node *list,int i,int x)
{struct node *new_node,*p,*c;int j=0,count=0;
 new_node=malloc(sizeof(struct node));
 p=list;c=list;
 for(;c!=NULL;c=c->next)
 {count++;}
 if(i<count)
 {if(i==0)
  {new_node->value=x;
   new_node->next=p;
   list=new_node;
    return list;
   }
 else
 {for(;;p=p->next)
 {if(j==(i-1))
  {new_node->value=x;
   new_node->next=p->next;
   p->next=new_node;
   break;
  }j++;}}}
  else
  {printf("index position doesn't exist \n");}
  return list;
 
}
//DELETES AN ELEMENT
struct node *delete(struct node *list)
{int n;
 struct node *cur,*prev;
 printf("enter the number to be deleted:\n");
 scanf("%d",&n);
 for(cur=list,prev=NULL;cur!=NULL && (cur->value)!=n;prev=cur,cur=cur->next);
 if(cur==NULL)
   {return list;}
 else if(prev==NULL)
   {list=list->next;return list;} 
 else
   {prev->next=cur->next;
   free(cur);
   return list;}
}
//DELETES AN ELEMENT AT THE INDEX "i"
struct node *pop(struct node *list)
{int i,n,count=0;
 struct node *cur,*prev,*c=list;
 printf("enter the index value to be deleted: \n");
 scanf("%d",&i);
 for(;c!=NULL;c=c->next)
 {count++;}
 if(i<count)
 {if(i==0)
 {list=list->next;
  return list;}
 else
 {cur=list;prev=NULL;
  for(n=1;n<=i;n++)
   {prev=cur;
    cur=cur->next;}
   prev->next=cur->next;
   free(cur);
   return list;}}
 else
  {printf("index value doesn't exist \n");
   return list;}
}
//SORTS THE LIST
struct node *sort(struct node *list)
{ struct node *p,*c;int val;
 p=list;c=list;
 for(;p!=NULL;p=p->next)
 {for(c=p;c!=NULL;c=c->next)
  {if((p->value)>(c->value))
   {val=p->value;
    p->value=c->value;
    c->value=val;
   }
  }
 }return list;}

//CREATES A LIST
struct node *create_list(struct node *list,int x)
{struct node *new_node;
 new_node=malloc(sizeof(struct node));
 if(new_node==NULL)
 {printf("error\n");
  exit(EXIT_FAILURE);
 }
 new_node->value=x;
 new_node->next=list;
 return new_node;
}
//PRINTS THE LIST
void print_list(struct node *l)
{printf("|");
 while(l!=NULL)
 {printf("%d |",l->value);
  l=l->next;}
}
//EXTENDS THE LIST
struct node *extend(struct node *list)
{ struct node *fir=NULL, *t;
 
 t=list;
 int n;
 printf("enter a series of no. which is to be added to original list(0 to terminate):");
 for(;;)
 {scanf("%d",&n);
 if (n==0)
 {break;}
 else
 {fir=create_list(fir,n);}} 
 printf("another list is");
 print_list(fir);
 printf("\n");
 while(t->next!=NULL)
{
t=t->next;
}
 t->next=fir;
 return list;
}
//MAIN FUNCTION
void main()
{struct node *first=NULL;
 struct node *k;
 
 int n,x,i,option,t=0;
 printf("enter a series of no.(0 to terminate):");
 for(;;)
 {scanf("%d",&n);
 if (n==0)
 {break;}
 else
 {first=create_list(first,n);}}
 k=first;
 printf("the list is:\n");
 print_list(k);
 printf("\n");
 
 while(t==0)
 {printf("choose an option(1:max,2:min,3:count,4:index,5:insert,6:append,7:reverse,8:delete,9:pop,10:sort,11:extend):\n");
  scanf("%d",&option);
  switch(option)
  {
  case 1:max(first);
         break;
  case 2:min(first);
         break; 
  case 3:count(first);
         break;
  case 4:index1(first);
         break;
  case 5:printf("enter the no. to inserted & its position(x,i):\n");
         scanf("%d,%d",&x,&i);
         first=insert(first,i,x);
         print_list(first);
         break;
  case 6:printf("enter the no. to be appended:\n");
         scanf("%d",&x);
         first=append(first,x);
         print_list(first);
         break;
  case 7:first=reverse(first);
         printf("\n");
         print_list(first);
         break;
  case 8:first=delete(first);
         print_list(first);
         break;
  case 9:first=pop(first);
         print_list(first);
         break;
  case 10:first=sort(first);
          print_list(first);
          break;
  case 11:first=extend(first);
          print_list(first);
          break;
} printf("\n");
  printf("Do you want to continue(y:0,n:1):\n");
  scanf("%d",&t);}}
